package chapter02;

import java.util.Scanner;

public class BookManager<loginUser, userName> {
    static Scanner sc = new Scanner(System.in);
    static String[][] users = new String[50][3];
    static String loginUser;
    static String[][] pubs = new String[50][3];
    static String[][] books = new String[50][5];

    public static void main(String[] args) {
        begin();
    }
    public static void begin(){
        System.out.println("欢迎来到龙院图书管理系统！");
        System.out.println("1.登录 2.注册");
        int log = sc.nextInt();
        switch (log) {
            case 1:
                login();
                break;
            case 2:
                register();
                break;
            default:
                System.out.println("系统退出");
                System.exit(0);
        }

    }

    private static void register() {
        System.out.println("请输入所属部门：");
        String deptName = sc.next();
        System.out.println("请输入用户名：");
        String userName = sc.next();
        System.out.println("请输入密码：");
        String passWord = sc.next();
        for (int i = 0; i < users.length; i++) {
            if (users[i][1] == null) {
                users[i][0] = deptName;
                users[i][1] = userName;
                users[i][2] = passWord;
                System.out.println("注册成功！");
                break;

            }
        }
        login();

    }

    private static void login() {
        String userName;
        while (true) {
            String passWord;
            System.out.println("请输入用户名：");
            userName = sc.next();
            System.out.println("请输入密码：");
            passWord = sc.next();
            boolean flag = false;
            for (int i = 0; i < users.length; i++) {
                if (userName.equals(users[i][1]) && passWord.equals(users[i][2])) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                System.out.println("登录成功！");
                break;
            } else {
                System.out.println("输入错误，请重新输入！");
            }

        }
        loginUser = userName;
        homePage();

    }

    private static void homePage() {
        System.out.println(loginUser+",欢迎您使用龙院书籍管理系统！");
        System.out.println("请输入数字进行选择：1.图书管理 2.出版社管理 3.退出登录 4.退出系统");
        int chance = sc.nextInt();
        switch (chance){
            case 1:
                bookmanager();
                break;
            case 2:
                pubmanager();
                break;
            case 3:
                System.out.println("已退出登录！");
                begin();
                break;
            case 4:
                System.out.println("系统退出成功！");
                System.exit(0);

        }
    }

    private static void pubmanager() {
        System.out.println("请输入：1.增加 2.删除 3.更新 4.根据出版社名称查询 5.查询所有出版社 6.返回上一级菜单");
        int c = sc.nextInt();
        switch (c){
            case 1:
                addpub();
                break;
            case 2:
                deletepub();
                break;
            case 3:
                updatepub();
                break;
            case 4:
                searchpubname();
                break;
            case 5:
                searchpubcom();
                break;
            case 6:
                homePage();
        }
    }

    private static void searchpubcom() {
        System.out.println("出版社\t地址 \t联系人\t");
        for (int i = 0; i < pubs.length; i++) {
            if(pubs[i][0] != null){
                System.out.println(pubs[i][0]+ "\t" +pubs[i][1] +"\t" +pubs[i][2] + "\t");

            }

        }
    }

    private static void searchpubname() {
        System.out.println("请输入出版社名称：");
        String pcname = sc.next();
        int i = getPub(pcname);
        if(i != -1){
            System.out.println("出版社名称："+pubs[i][0]);
            System.out.println("出版社地址："+pubs[i][1]);
            System.out.println("出版社联系人："+pubs[i][2]);
        }else {
            System.out.println("该出版社不存在！");
        }
    }

    private static int getPub(String pcname) {

        return 0;
    }

    private static void updatepub() {
        System.out.println("请输入要更新的出版社名称：");
        String psname = sc.next();
        int i = getPub(psname);
        printPubCom(i);
        System.out.println("请输入要更新的地址：");
        String a = sc.next();
        System.out.println("请输入要更新的联系人姓名：");
        String b = sc.next();
        boolean c = updatec(i,a,b);
        if(c){
            System.out.println("更新成功！");
            printPubCom(i);
        }else {
            System.out.println("更新失败！");
        }
    }


    private static boolean updatec(int i, String a, String b) {
        boolean c = false;
        pubs[i][1] = a;
        pubs[i][2] = b;
        c = true;
        return c;
    }

    private static void deletepub() {
        System.out.println("请输入要删除的出版社名称：");
        String puname = sc.next();
        boolean s = deletename(puname);
        if(s){
            System.out.println("删除成功！");
        }else {
            System.out.println("删除失败！");
        }
    }

    private static boolean deletename(String puname) {

        return false;
    }

    private static void addpub() {
        System.out.println("请输入出版社名称：");
        String pcname  = sc.next();
        System.out.println("请输入出版社地址：");
        String pcaddress = sc.next();
        System.out.println("请输入出版社联系人：");
        String pcperson = sc.next();
        boolean p = addname(pcname,pcaddress,pcperson);
        if(p){
            System.out.println("出版社添加成功！");
        }else {
            System.out.println("出版社添加失败！");
        }
    }

    static void printPubCom(int i){
        System.out.println("出版社\t地址                     \t联系人\t");
        System.out.println(pubs[i][0] + "\t");
        System.out.println(pubs[i][1] + "\t");
        System.out.println(pubs[i][2] + "\t");
    }
    private static boolean addname(String pcname, String pcaddress, String pcperson) {
        boolean p  = false;
        if(getPub(pcname) == -1){
            int index = getIndex();
            pubs[index][0] = pcname;
            pubs[index][1] = pcaddress;
            pubs[index][2] = pcperson;
            p = true;
        }
        return p;
    }

    private static int getIndex() {
        int index = -1;
        for (int i = 0; i < pubs.length; i++) {
            if(pubs[i][0] == null){
                index = i;
                break;
            }

        }
        return index;
    }

    private static void bookmanager() {
        while (true){
            System.out.println("请输入：1.增加 2.删除 3.更新 4.查询 5.返回上一级菜单");
            int d = sc.nextInt();
            switch (d){
                case 1:
                    addbook();
                    break;
                case 2:
                    deletebook();
                    break;
                case 3:
                    updatebook();
                    break;
                case 4:
                    searchbook();
                    break;
                case 5:
                    homePage();
            }
        }
    }
    static void searchbookISBN(String isbn){
        System.out.println("ISBN\t书名               \t价格\t出版社             \t作者\t");
        for (int i = 0; i < books.length; i++) {
            if(isbn.equals(books[i][0])){
                System.out.println(books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + books[i][4] + "\t");
                break;
            }

        }
    }

    private static void searchbook() {
        while (true){
            System.out.println("请输入查询种类：1.ISBN 2.书名（模糊） 3.出版社 4.作者 5.价格范围 6.查询所有 7.返回上一级");
            int a = sc.nextInt();
            String author;
            if(a == 1){
                System.out.println("请输入ISBN号：");
                author = sc.next();
                searchbookISBN(author);
            }else if(a == 2){
                System.out.println("请输入书名：");
                author = sc.next();
                searchbookISBN(author);
            }else if(a == 3){
                System.out.println("请输入出版社前的数字进行选择：");
                int w = sc.nextInt();
                if(w >= 1&& w <=getNum()){

                }

            }
        }
    }

    private static int getNum() {
        return 0;
    }

    private static void updatebook() {
    }

    private static void deletebook() {
    }

    private static void addbook() {
    }
}